package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.vo.ShippingVo;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.*;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@Controller
@RequestMapping("/cargo/shipping/")
public class ShippingController extends BaseController {

    @Reference
    private ShippingService shippingService;

    @Reference
    private PackingService packingService;

    //展示委托列表
    @RequestMapping("/list")
    public String shippingList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
       PageInfo<Shipping> pageInfo= shippingService.findAll(getLoginCompanyId(),pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);
         return "cargo/shipping/shipping-list";
    }



    //去新建列表
    @RequestMapping("/toAdd")
    public String toAdd(String packingListId){
        //需要判断增加的委托单是来源于装箱单还是委托单列表
        if(!StringUtils.isEmpty(packingListId)){
            //有值表示由装箱列表中来,则部分数据来源于装箱列表,需要有数据的回显
            Packing packing=packingService.findByPLid(packingListId);
            Shipping shipping = new Shipping();
            //封装数据
            //id
            shipping.setShippingOrderId(packing.getPackingListId());
            //运输方式
            if(!"SEA".equals(packing.getTransportMode())||!"AIR".equals(packing.getTransportMode())){
                packing.setTransportMode("SEA");
            }
            shipping.setOrderType(packing.getTransportMode());
            //货主
            shipping.setShipper(getLoginCompanyName());
            //提单抬头
            shipping.setConsignee(packing.getConsignee());
            //正本通知人
            shipping.setNotifyParty(packing.getConsignee());
            //信用证
            shipping.setLcNo(packing.getLcno());
            //装运港
            shipping.setPortOfLoading(packing.getShipmentPort());
            //转船港(不需要封装)
            //目的港
            shipping.setPortOfDischar(packing.getDestinationPort());
            //装期(不需要封装)
            //效期(不需要封装)
            //唛头(完了)
            String marks = packing.getMarks();

            request.setAttribute("marks",marks);
            request.setAttribute("shipping",shipping);
            return "cargo/shipping/shipping-add";
        }

        //为空返回到装箱列表
        return "redirect:/cargo/packing/list.do";
    }

    //增加委托单
    @RequestMapping("/edit")
    public String add(Shipping shipping){
        //封装控制器相关数据
        shipping.setCompanyId(getLoginCompanyId());
        shipping.setCompanyName(getLoginCompanyName());
        User loginUser = getLoginUser();
        shipping.setCheckBy(loginUser.getUserName());
        shipping.setCreateDept(loginUser.getDeptName());
        shippingService.addShipping(shipping);
        return "redirect:/cargo/shipping/list.do";
    }

    //查看委托表
    @RequestMapping("/toView")
    public String toView(String shippingOrderId){
        Shipping shipping = shippingService.findBySOId(shippingOrderId);
        request.setAttribute("shipping",shipping);
        return "cargo/shipping/shipping-view";
    }

    //删除委托单
    @RequestMapping("/delete")
    public String delete(String shippingOrderId){
        shippingService.deleteBySId(shippingOrderId);
        return "redirect:/cargo/shipping/list.do";
    }

    //以PDF的方式下载委托单
    @RequestMapping("/printPdf")
    @ResponseBody
    public void exportPdf(String shippingOrderId) throws JRException, IOException {
        //1、得到Jasper文件的输入流
        InputStream inputStream = session.getServletContext().getResourceAsStream("/jasper/shipping.jasper");

        //2、把模板与数据结合，得到JasperPrint对象（数据+模板结合）
        //封装委托表的数据
        ShippingVo shippingVo=new ShippingVo();
        Packing packing = packingService.findByPLid(shippingOrderId);
        Shipping shipping = shippingService.findBySOId(shippingOrderId);
         //封装shippingVo
         shippingVo.setShippingOrderId(shipping.getShippingOrderId());
         shippingVo.setOrderType(shipping.getOrderType());
         shippingVo.setShipper(shipping.getShipper());
         shippingVo.setConsignee(shipping.getConsignee());
         shippingVo.setNotifyParty(shipping.getNotifyParty());
         shippingVo.setLcNo(shipping.getLcNo());
         shippingVo.setPortOfLoading(shipping.getPortOfLoading());
         shippingVo.setPortOfTrans(shipping.getPortOfTrans());
         shippingVo.setPortOfDischar(shipping.getPortOfDischar());
         shippingVo.setLoadingDate(shipping.getLoadingDate());
         shippingVo.setLimitDate(shipping.getLimitDate());
         shippingVo.setIsBatch(shipping.getIsBatch());
         shippingVo.setIsTrans(shipping.getIsTrans());
         shippingVo.setCopyNum(shipping.getCopyNum());
         shippingVo.setRemark(shipping.getRemark());
         shippingVo.setSpecialConditio(shipping.getSpecialConditio());
         shippingVo.setFreight(shipping.getFreight());
         shippingVo.setCheckBy(shipping.getCheckBy());
         shippingVo.setState(shipping.getState());
         shippingVo.setCheckBy(shipping.getCheckBy());
         shippingVo.setCreateDept(shipping.getCreateDept());
         shippingVo.setCompanyId(shipping.getCompanyId());
         shippingVo.setCompanyName(shipping.getCompanyName());
         shippingVo.setCreateTime(shipping.getCreateTime());
         shippingVo.setExportIds(packing.getExportIds());
         shippingVo.setExportNos(packing.getExportNos());
         shippingVo.setPackingTime(packing.getPackingTime());
         shippingVo.setTotalVolume(packing.getTotalVolume());
         shippingVo.setNetWeights(packing.getNetWeights());
         shippingVo.setGrossWeights(packing.getGrossWeights());
         shippingVo.setMarks(packing.getMarks());
         shippingVo.setDescription(packing.getDescription());
         shippingVo.setPackingMoney(packing.getPackingMoney());

        //将对象转为map<String,Object>集合
        Map<String, Object> map = BeanMapUtils.beanToMap(shippingVo);


        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, map,new JREmptyDataSource());

        //4、设置响应头，下载
        response.setHeader("content-disposition","attachment;filename=Shipping.pdf");
        //3、把PDF文件输出
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());

    }


}
